package com.cbgolf.oa.activity.message;

import static com.cbgolf.oa.util.AnimaUtil.stopLoading;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import com.cbgolf.oa.R;
import com.cbgolf.oa.adapter.MessageAdapter;
import com.cbgolf.oa.app.AppManager;
import com.cbgolf.oa.base.BaseActivity;
import com.cbgolf.oa.entity.MessageBean;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.NetUtils;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.DialogUtil;
import com.cbgolf.oa.util.Util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by Administrator on 2017/9/2.
 */

public class PushHistoryActivity extends BaseActivity implements MessageAdapter.onMessageListener,
    SwipeRefreshLayout.OnRefreshListener {

  //top
  @ViewInject(R.id.topBack_ll)
  private View topBack;
  @ViewInject(R.id.title_tv)
  private TextView topTitle;
  //body
  @ViewInject(R.id.a_push_history_refresh_layout)
  private SwipeRefreshLayout refreshLayout;
  @ViewInject(R.id.a_push_history_listview)
  private ListView listView;
  //error
  @ViewInject(R.id.g_error_ll)
  private View errorView;
  @ViewInject(R.id.g_error_tv)
  private TextView errorTv;
  @ViewInject(R.id.g_error_reload_tv)
  private TextView reLoadTv;
  @ViewInject(R.id.g_error_loading_tv)
  private TextView loadingTv;
  //var
  private final int pageSize = 15;
  private int page = 0;
  private List<MessageBean> listData = new ArrayList<>();
  private MessageAdapter adapter;


  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.a_push_history);
  }

  @Override
  protected void findViews() {
    x.view().inject(this);
  }

  @Override
  protected void setViews() {
    topTitle.setText("推送历史");
    adapter = new MessageAdapter(context, this);
    listView.setAdapter(adapter);
  }


  @Override
  protected void setListener() {
    refreshLayout.setOnRefreshListener(this);
    topBack.setOnClickListener(v -> finish());
    reLoadTv.setOnClickListener(v -> loadData());
  }

  @Override
  protected void loadData() {
    super.loadData();
    if (!Util.isNetWorkConnected()) {
      showError(NONET);
      return;
    }
    String url = WebAPI.msg_select_get;
    Map<String, String> map = new HashMap<>(2);
    map.put("pageSize", pageSize + "");
    map.put("currentPage", "0");
    showLoading();
    Web.getOK(url, map, new NetCallBack(response -> {
      if (NetUtils.SUCCESS != response.statusCode) {
        showError(NETERROR);
        return;
      }
      if (Util.isNull(response.result)) {
        showError(NODATA);
        return;
      }
      List<MessageBean> list = DataUtil.getList(response, MessageBean.class);
      if (!Util.listIsNull(list)) {
        showData();
        listData.clear();
        listData.addAll(list);
        adapter.setData(listData);
        adapter.upDataUI();
      } else {
        showError(NODATA);
      }

    }));


  }

  private void showLoading() {
    if (!refreshLayout.isRefreshing()) {
      AnimaUtil.showLoading(context, loadingTv);
    }

  }

  private void showData() {
    stopLoading();
    refreshLayout.setRefreshing(false);
    refreshLayout.setVisibility(View.VISIBLE);
    errorView.setVisibility(View.GONE);
  }

  private final int NONET = 1;
  private final int NETERROR = 2;
  private final int NODATA = 3;

  private void showError(int type) {
    stopLoading();
    if (null != refreshLayout) {
      refreshLayout.setRefreshing(false);
    }
    if (listData.size() > 0) {
      switch (type) {
        case NONET:
          Util.showNoNet();
          break;
        case NETERROR:
          Util.showNetError();
          break;
        case NODATA:
          Util.showNoData();
          break;
        default:
          break;
      }
      return;
    }
    refreshLayout.setVisibility(View.GONE);
    errorView.setVisibility(View.VISIBLE);
    reLoadTv.setVisibility(View.VISIBLE);
    switch (type) {
      case NONET:
        errorTv.setText(getResources().getString(R.string.no_net));
        break;
      case NETERROR:
        errorTv.setText(getResources().getString(R.string.net_error));
        break;
      case NODATA:
        errorTv.setText(getResources().getString(R.string.no_data));
        break;
      default:
        break;
    }


  }

  @Override
  public void modify(int position) {

    AppManager.getNewInstance().finishActivity(MessageActivity.class);
    Intent intent = new Intent(context, MessageActivity.class);
    intent.putExtra("msg", listData.get(position).messageContent);
    intent.putExtra("id", listData.get(position).id);
    intent.putExtra("isEdit", true);
    startActivity(intent);
    finish();


  }

  @Override
  public void del(final int position) {
    String msg = "是否确定删除该信息？";
    DialogUtil.showDelDialog(context, "", msg, new DialogUtil.onSubmitListener() {
      @Override
      public void doSubmit() {
        delMsg(position);
      }
    });
  }

  private void delMsg(final int position) {
    if (!Util.isNetWorkConnected()) {
      Util.showNoNet();
      return;
    }
    String id = "";
    try {
      id = listData.get(position).id;
    } catch (Exception e) {

    }
    if (Util.isNull(id)) {
      Util.showNetError();
      return;
    }
    String url = WebAPI.msg_del_delete + "/" + id;
    Web.deleteOk(url, new NetCallBack(response -> {
      if (NetUtils.SUCCESS != response.statusCode) {
        Util.showNetError();
        return;
      }
      listData.remove(position);
      adapter.upDataUI();
    }));
  }

  @Override
  public void onRefresh() {
    loadData();
  }
}
